package test;

import util.DBUtil;

import java.sql.*;
import java.util.Arrays;

public class GetsTablefieldsUtil {
    public static String[] getAllcolumns(String tables) {
        // 数据库连接信息
        String[] strings=new String[20];
        String url = "jdbc:mysql://localhost:3306/testdatabase1?useUnicode=true&characterEncoding=gbk";
        String username = "root";
        String password = "hewenhui";
        try (Connection connection = DBUtil.getConn()) {
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet columns = metaData.getColumns(null, null, tables, null);
            int i=0;
            while (columns.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                strings[i++]=columnName;
//                String columnType = columns.getString("TYPE_NAME");
//                int columnSize = columns.getInt("COLUMN_SIZE");
//                boolean isNullable = columns.getBoolean("NULLABLE");
//                System.out.println(columnName);
//                System.out.println("Column Type: " + columnType);
//                System.out.println("Column Size: " + columnSize);
//                System.out.println("Is Nullable: " + isNullable);
//                System.out.println("---------------------");
            }
            String[] strings1= Arrays.copyOf(strings,i);
            return strings1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String[] personnels = GetsTablefieldsUtil.getAllcolumns("personnel");
        for (String str:personnels) {
            System.out.println(str);
        }
    }
}
